#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qmyvideowidget.h"
#include <QString>
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QMediaPlayer>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 添加播放器
    player = new QMediaPlayer(this);            // 创建视频播放器
    player->setNotifyInterval(1000);            // 信息更新周期
    videoWidget = new QmyVideoWidget(this);           // 新建播放容器
    videoWidget->resize(780,430);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButtonAddFile_clicked()
{
    QString currentPath = QDir::homePath();     // 获取系统当前目录
    QString openFiledTitle = "请选择视频文件";
    QString fileFilter = "wmv文件(*.wmv);;mp4文件(*.mp4);;所有文件(.)";       // 过滤器
    QString filedLg = QFileDialog::getOpenFileName(this,openFiledTitle,currentPath,fileFilter);

    if(filedLg.isEmpty()) return;

    // 获取文件信息
    QFileInfo fileInfo(filedLg);

    // 改变标签，显示文件名
    ui->labelVideoName->setText(fileInfo.fileName());


    //player->setMedia(QUrl::fromLocalFile(QString(currentPath + "/" + fileInfo.fileName()).replace(R"(/)",R"(\\)")));
    player->setMedia(QUrl::fromLocalFile("C:\\Users\\ASUS\\Videos\\video.mp4"));
    player->setVideoOutput(videoWidget);
    videoWidget->show();
    player->play();
}

void MainWindow::on_pushButtonPlayer_clicked()
{
    // qInfo()<<"clicked";
    this->player->play();
}

void MainWindow::on_pushButtonPause_clicked()
{
    this->player->pause();
}
